package com.danceforyou.common.service.impl;

import com.danceforyou.common.dao.ConfigDAO;
import com.danceforyou.common.model.ConfigDO;
import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.service.ConfigService;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.common.vo.ConfigVO;
import com.danceforyou.user.model.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
* 类名称：ConfigServiceImpl
* 创建人：danceforyou
* 创建时间：2021年01月10日
* 版本号：1.0v
*/
@Service("configService")
public class ConfigServiceImpl extends BaseServiceImpl<Long, ConfigDO, ConfigVO, ConfigDAO> implements ConfigService {
    @Autowired
    private ConfigDAO configDAO;
    @Autowired(required = false)
    private HttpSession session;

    @Override
    public ConfigDAO getDao() {
        return configDAO;
    }

    @Override
    public void saveConfig(Map<String, String> configMap) {
        UserDO loginUser = (UserDO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        for (Map.Entry<String, String> entry : configMap.entrySet()) {
            ConfigDO config = this.getByProperty("configText", entry.getKey());
            if (config != null) {
                config.setConfigValue(entry.getValue());
                this.update(config);
            } else {
                if (Constants.CONFIG_SHOW_RANK.equals(entry.getKey()) || Constants.CONFIG_PRECISE_TAX.equals(entry.getKey()) || Constants.CONFIG_SUPERVISOR_ALL.equals(entry.getKey())|| Constants.CONFIG_CUSTOMER_VALID_DAY.equals(entry.getKey()) || Constants.CONFIG_IP_WHITE_LIST.equals(entry.getKey()) || Constants.CONFIG_MAX_LOGIN_FAILED_NUMBER.equals(entry.getKey())) {
                    config = new ConfigDO();
                    config.setConfigText(entry.getKey());
                    config.setConfigValue(entry.getValue());
                    this.insert(config);
                }
            }
        }
    }
}
